package com.weng.db.demo;

import java.sql.*;
import java.util.List;

public class DMJDBC {

    public static void main(String[] args) {
        Connection con;
        String driver = "dm.jdbc.driver.DmDriver";
        String jdbcUrl = "jdbc:dm://172.16.0.82:5236/aep";
        String user = "SYSDBA";
        String password = "SYSDBA";
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(jdbcUrl,user,password);

            List<String>  tableNames = DbUtil.getTableNames(jdbcUrl, "SYSDBA",con.getMetaData());
            System.out.println(tableNames.size());

            if(!con.isClosed()){
                System.out.println("Succeeded connecting to the Database!");
            }
            Statement statement = con.createStatement();
            //要执行的SQL语句
            String sql = "select * from TT";
            //3.ResultSet类，用来存放获取的结果集！！
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("-----------------");
            System.out.println("执行结果如下所示:");
            System.out.println("-----------------");
            System.out.println("编号" + "\t" + "姓名");
            System.out.println("-----------------");

            String id = null;
            String name = null;
            while(rs.next()){
                id = rs.getString("A");

                System.out.println(id + "\t" + id);
            }
            rs.close();
            con.close();
        } catch(ClassNotFoundException e) {
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch(SQLException e) {
            //数据库连接失败异常处理
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            System.out.println("数据库数据成功获取！！");
        }
    }


}
